// pages/smartlife/bus_suzhou/bus_suzhou.js
var common = require('../../../utils/common.js');
var startTime = null
var endTime = null
var timeId = null
Page({
  data:{
    status:0,
    buslineList: [],
    stationList: [],
    searchList: [],
    buslineIndex:-1,
    stationIndex:-1,
    searchText:''
  },
  onLoad:function(options){
    var that=this
    try {
      var busline = wx.getStorageSync('busline_sz')
      var station = wx.getStorageSync('station_sz')
      var appUsed = wx.getStorageSync('appused')
      if (!appUsed) {
          // 跳转到app使用引导页面
          wx.navigateTo({
            url: '/pages/smartlife/guide/guide'
          })
          wx.setStorageSync('appused', true)
      }
      if (busline) {
          that.setData({
            buslineList:busline
          })
      }
      if (station) {
          that.setData({
            stationList:station
          })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  onShareAppMessage: function () {
    return {
      title: '苏州最方便的实时公交系统',
      path: '/pages/smartlife/bus_suzhou/bus_suzhou'
    }
  },
  titleChange:function(e){
    var that = this
    if(that.data.status!=e.currentTarget.dataset.status){
      that.setData({
        searchText:'',
        searchList:[]
      })
    }
    that.setData({
      status:e.currentTarget.dataset.status
    })
  },
  searchClick:function(e){
    var that = this
    var index = e.currentTarget.dataset.index
    var list = []
    if(that.data.status==0){
      list = that.data.buslineList
    }else{
      list = that.data.stationList
    }
    var item = that.data.searchList[index]
    for(var i=0;i<list.length;i++){
      // console.log(JSON.stringify(item))
      if(JSON.stringify(list[i])==JSON.stringify(item)){
        // console.log('same')
        list.splice(i,1)
        break
      }
    }
    list.unshift(item)
    if(that.data.status==0){
      that.setData({
        buslineList:list
      })
    }else{
      that.setData({
        stationList:list
      })
    }
    
    try {
      if(that.data.status==0){
        wx.setStorageSync('busline_sz', that.data.buslineList)
      }else{
        wx.setStorageSync('station_sz', that.data.stationList)
      }
    } catch (e) {    
    }
    // console.log(that.data.searchList[index])
    if(that.data.status==0){
      wx.navigateTo({
        url: '/pages/smartlife/bus_suzhou/busline/busline?guid='+that.data.searchList[index].Guid+'&lineName='+that.data.searchList[index].LName+'&lineDirection='+that.data.searchList[index].LDirection
      })
    }else{
      wx.navigateTo({
        url: '/pages/smartlife/bus_suzhou/station/station?noteGuid='+that.data.searchList[index].NoteGuid+'&name='+that.data.searchList[index].Name
      })
    }
    that.setData({
      searchText:'',
        searchList:[]
    })
  },
  locationClick:function(e){
    // console.log(e)
    wx.navigateTo({
      url: '/pages/smartlife/bus_suzhou/location/location'
    })
  },
  buslineClick:function(e){
    if(this.endTime  - this.startTime < 350) {
      var that = this
      var index = e.currentTarget.dataset.index
      wx.navigateTo({
        url: '/pages/smartlife/bus_suzhou/busline/busline?guid='+that.data.buslineList[index].Guid+'&lineName='+that.data.buslineList[index].LName+'&lineDirection='+that.data.buslineList[index].LDirection
      })
      var list = []
      list = that.data.buslineList
      var item = that.data.buslineList[index]
      for(var i=0;i<list.length;i++){
        if(JSON.stringify(list[i])==JSON.stringify(item)){
          list.splice(i,1)
          break
        }
      }
      list.unshift(item)
      that.setData({
        buslineList:list
      })
      try {
        wx.setStorageSync('busline_sz', that.data.buslineList)
      } catch (e) {    
      }
    }
  },
  stationClick:function(e){
    if(this.endTime  - this.startTime < 350) {
      var that = this
      var index = e.currentTarget.dataset.index
      wx.navigateTo({
        url: '/pages/smartlife/bus_suzhou/station/station?noteGuid='+that.data.stationList[index].NoteGuid+'&name='+that.data.stationList[index].Name
      })
      var list = []
      list = that.data.stationList
      var item = that.data.stationList[index]
      for(var i=0;i<list.length;i++){
        if(JSON.stringify(list[i])==JSON.stringify(item)){
          list.splice(i,1)
          break
        }
      }
      list.unshift(item)
      that.setData({
        stationList:list
      })
      try {
        wx.setStorageSync('station_sz', that.data.stationList)
      } catch (e) {    
      }
    }
  },
  buslineTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  stationTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  buslineTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  stationTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  deleteBusline:function(e){
    var that = this
    that.setData({
      buslineIndex:e.currentTarget.dataset.index
    })

    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function(res) {
        switch(res.tapIndex){
          case 0:
            var list = that.data.buslineList
            list.splice(e.currentTarget.dataset.index,1)
            // console.log('删除-----'+e.currentTarget.dataset.index)
            that.setData({
              buslineIndex:-1,
              buslineList:list
            })
            try {
              wx.setStorageSync('busline_sz', list)
            } catch (e) {    
            }
          break;
          case 1:

          break;
        }
      },
      fail: function(res) {
        that.setData({
          buslineIndex:-1
        })
      }
    })
  },
  deleteStation:function(e){
    var that = this
    that.setData({
      stationIndex:e.currentTarget.dataset.index
    })

    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function(res) {
        switch(res.tapIndex){
          case 0:
            var list = that.data.stationList
            list.splice(e.currentTarget.dataset.index,1)
            // console.log('删除-----'+e.currentTarget.dataset.index)
            that.setData({
              stationIndex:-1,
              stationList:list
            })
            try {
              wx.setStorageSync('station_sz', list)
            } catch (e) {    
            }
          break;
          case 1:

          break;
        }
      },
      fail: function(res) {
        that.setData({
          stationIndex:-1
        })
      }
    })
  },
  searchInput:function(e){
    // console.log(e.detail.value)
    var that=this
    clearTimeout(that.timeId)
    that.timeId = setTimeout(function(){
      if(e.detail.value==''){
        that.setData({
          searchList:[]
        })
        return
      }
      if(that.data.status==0){
        wx.request({
          url: common.trafficUrl()+'busRoute',
          data: {
              keyword:e.detail.value,
              city:'suzhou'
          },
          // header: {}, // 设置请求的 header
          success: function(res){
            if(res.data.errorCode==0){
              // console.log(res.data.data.list)
              that.setData({
                searchList:res.data.data.list
              })
            }else{
                
            }
          }
        })
      }else{
        wx.request({
          url: common.trafficUrl()+'busStation',
          data: {
              keyword:e.detail.value,
              city:'suzhou'
          },
          // header: {}, // 设置请求的 header
          success: function(res){
            if(res.data.errorCode==0){
              // console.log(res.data.data.list)
              that.setData({
                searchList:res.data.data.list
              })
            }else{
                
            }
          }
        })
      }
    // console.log(e.detail.value)
    },500)
  }
})